package org.dylan.evernote;

import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import com.evernote.edam.error.EDAMNotFoundException;
import com.evernote.edam.error.EDAMSystemException;
import com.evernote.edam.error.EDAMUserException;
import com.evernote.edam.type.Data;
import com.evernote.edam.type.Note;
import com.evernote.edam.type.Resource;
import com.evernote.edam.type.ResourceAttributes;
import com.evernote.thrift.TException;

/**
 *
 *
 * @Copyright: Copyright (c) 2013 FFCS All Rights Reserved 
 * @Company: 北京福富软件有限公司 
 * @author 陈作朋 Oct 25, 2013
 * @version 1.00.00
 * @history:
 * 
 */
public class CreateNote {
	
	public static String getBody(String noteBody){
		String result="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		result += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">";
		result += "<en-note>" + noteBody + "</en-note>";
		return result;
	}

	public static void createTextNote() throws EDAMUserException, EDAMSystemException, EDAMNotFoundException, TException{
		EDAMContext context=new EDAMContext();
		Note note=new Note();
		note.setTitle("note-"+new Date().getTime());
		
        note.setContent(getBody("abc"));
        note.setTagNames(Arrays.asList(new String[]{"keepnote","aaa"}));
		Note createdNote=context.getNoteStore().createNote(note);
		System.out.println("note guid:"+createdNote.getGuid());
	}
	
	public static void createNoteWithAttachs() throws Exception{
		EDAMContext context=new EDAMContext();
		Note note=new Note();
		note.setTitle("note-"+new Date().getTime());
		
	    String fileName = "page.html";
//	    String mimeType = "application/pdf";
	    String mimeType="text/xhtml+xml";
		
		String path="/home/dylan/Data/keepnote/Linux/command/ssh/ssh 隧道/实战ssh端口转发/page.html";
		
		byte[] rData=getData(path);
		
	    Data data = new Data();
	    data.setSize(rData.length);
	    data.setBodyHash(MessageDigest.getInstance("MD5").digest(rData));
	    data.setBody(rData);
		
	    Resource resource = new Resource();
	    resource.setData(data);
	    resource.setMime(mimeType);
	    ResourceAttributes attributes = new ResourceAttributes();
	    attributes.setAttachment(true);
	    attributes.setFileName(fileName);
	    resource.setAttributes(attributes);

	    note.addToResources(resource);

		
	    String hashHexData=EDAMContext.bytesToHex(resource.getData().getBodyHash());
	    
	    
	    
		String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		xml += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">";
		xml += "<en-note>";
//		xml+="<en-media type=\"application/pdf\" hash=\"" + hashHexData + "\"/>";
		xml+="<en-media type=\"text/xhtml+xml\" hash=\"" + hashHexData + "\"/>";
		xml+="</en-note>";
		
        note.setContent(xml);
		
		Note createdNote=context.getNoteStore().createNote(note);
		System.out.println("note guid:"+createdNote.getGuid());
	}
	
	public static byte[] getData(String path) throws IOException{
		return FileUtils.readFileToByteArray(new File(path));
	}

	public static void main(String[] args) throws Exception{
//		createNoteWithAttachs();
		createTextNote();
	}

}
